package Practice;

public class ThreePrintSequence {
    public static int flag = 0;

    public static void main(String[] args) {
        Object locker = new Object();

        Thread a = new Thread(() -> {
            synchronized (locker) {
                while(flag!=2) {
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("a");
                flag = 0;
                locker.notifyAll();
            }
        });
        Thread b = new Thread(() -> {
            synchronized (locker) {
                while(flag!=1) {
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("b");
                flag = 2;
                locker.notifyAll();
            }
        });
        Thread c = new Thread(() -> {
            synchronized (locker) {
                while(flag!=0) {
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("c");
                flag = 1;
                locker.notifyAll();
            }
        });
        a.start();
        b.start();
        c.start();
    }
}
